﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace boda.utilities.Times
{
    public class DateTimeCom
    {
        /// <summary>
        /// 时间差计算
        /// </summary>
        /// <param name="tim"></param>
        /// <returns></returns>
        public static long TimeSecondDiff(DateTime tim)
        {
            return (DateTime.Now.Ticks - tim.Ticks) / 10000000;
        }

        public static int TodaySenconds()
        {
            TimeSpan totalSeconds = DateTime.Now.TimeOfDay;
            int seconds = (int)totalSeconds.TotalSeconds;
            return seconds;
        }

        //生成unix格式时间
        public static long GetTimeStamp(bool isUtc = true, bool isMilisec = false)
        {
            try
            {
                TimeSpan timespan = (isUtc ? DateTime.Now.ToUniversalTime() : DateTime.Now) - new DateTime(1970, 1, 1, 0, 0, 0);
                var res = isMilisec ? (long)timespan.TotalMilliseconds : (long)timespan.TotalSeconds;
                return res;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public static long GetTargetTimeStamp(DateTime dateTime, bool isMilisec = false)
        {
            try
            {
                TimeSpan timespan = dateTime - new DateTime(1970, 1, 1);
                var res = isMilisec ? (long)timespan.TotalMilliseconds : (long)timespan.TotalSeconds;
                return res;
            }
            catch (Exception)
            {
                return -1;
            }
        }
    }
}
